/**
 * localStorage工具函数
 */

export const storage = {
  // 获取localStorage中的值
  get<T>(key: string, defaultValue: T | null = null): T | null {
    try {
      const item = localStorage.getItem(key);
      return item ? JSON.parse(item) : defaultValue;
    } catch (error) {
      console.error(`Error getting item from localStorage with key "${key}":`, error);
      return defaultValue;
    }
  },

  // 设置localStorage中的值
  set<T>(key: string, value: T): void {
    try {
      localStorage.setItem(key, JSON.stringify(value));
    } catch (error) {
      console.error(`Error setting item in localStorage with key "${key}":`, error);
    }
  },

  // 移除localStorage中的值
  remove(key: string): void {
    try {
      localStorage.removeItem(key);
    } catch (error) {
      console.error(`Error removing item from localStorage with key "${key}":`, error);
    }
  },

  // 清空localStorage
  clear(): void {
    try {
      localStorage.clear();
    } catch (error) {
      console.error('Error clearing localStorage:', error);
    }
  }
};